<template>
  <el-container style="margin-top: 10px">
    <el-header>
      <el-card style="height: 75px">
        <el-button
          icon="el-icon-plus"
          size="mini"
          type="primary"
          @click="dialogVisible = true"
        >
          添加问题
        </el-button>
      </el-card>
    </el-header>

    <el-main>
      <el-card>
        <el-table v-loading="loading" :data="questionList">
          <el-table-column prop="question"   label="题目名称" align="center" > </el-table-column>
          <el-table-column align="center" prop="pick_type" label="题目类型" >
            <template slot-scope="scope" >
              <div>{{scope.row.pick_type ==0?'问答题':scope.row.pick_type == 1?'单选题':'多选题'}}</div> 
            </template>
          </el-table-column>
          <el-table-column align="center" prop="answer_count" label="选项数量" /> 

          <el-table-column label="操作" align="center">
            <template slot-scope="scope" >
            
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="hanldePreview(scope.row.id)"
                v-if="scope.row.pick_type"
              >查看选项</el-button>
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                 @click="hanldePreview(scope.row.id)"
                v-else
              >无选项</el-button>

              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-main>

    <el-footer style="height: 85px">
      <el-card>
        <el-pagination
          style="text-align: center;"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @current-change="handleCurrentChange"
        />
      </el-card>
    </el-footer>
    <el-dialog title="添加题目" :visible.sync="dialogVisible" @close="dialogClose">
      <el-form ref="dialog" :model="addQuestionObj" :rules="rule" label-position="" label-width="120px">
        <el-form-item label="题目内容:" prop="question">
          <el-input v-model="addQuestionObj.question" />
        </el-form-item>
        <el-form-item label="题目类型:" prop="pick_type">
          <el-select v-model="addQuestionObj.pick_type" @change="picktypechange">
            <el-option
              v-for="item in question_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-show="pickShow" label="添加选项:" />
        <el-form-item v-for="(item,index) in pick" v-show="pickShow" :key="index" :label="item.label">
          <el-input v-model="item.value" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-if="showBtn" class="iconfont iconicon_add" @click="addQuestions">添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-container>
</template>

<script>
import { addQuestion, getQuestionList, delQuestion, detailQuestion } from '@/api/setting'
export default {
  data() {
    return {
      loading: false,
      dialogVisible: false,
      pickShow: false,
      showBtn: true,
      questionList: [],
      total: 0,
      page: 1,
      pageSize: 10,
      question_type: [
        { label: '问答题', value: 0 },
        { label: '单选题', value: 1 },
        { label: '多选题', value: 2 }
      ],
      list:['A','B','C','D','E','F','G','H','I','J','K'],
      pick: [
        { label: 'A', value: '' },
        { label: 'B', value: '' },
        { label: 'C', value: '' },
        { label: 'D', value: '' }
      ],
      addQuestionObj: {
        question: '',
        answer_type: 2,
        answer: '',
        pick_type: 0
      },
      rule: {
        // subject: [
        //     {required: true, message: '科目类型为必填项'},
        //     {type: 'number', message: '科目类型为数字', trigger: 'blur'}
        // ],
        question: [
          { required: true, message: '题目内容为必填项', trigger: 'blur' }
          // {min:5, max: 50, message: '练车地址字符长度在5到50个字符之间', trigger: 'change'}
        ]
      }
    }
  },
  mounted() {
    this.getQuestionList()
  },
  methods: {
    getQuestionList() {
      this.loading = true
      const params = {
        page: this.page,
        page_size: this.pageSize
      }
      getQuestionList(params).then(res => {
        this.questionList = res.data.list
        this.total = res.data.paginate.total
        this.loading = false
        this.pick=[
        { label: 'A', value: '' },
        { label: 'B', value: '' },
        { label: 'C', value: '' },
        { label: 'D', value: '' }
      ]
      })
    },
    handleCurrentChange(e) {
      this.page = e
      this.getQuestionList()
    },
    picktypechange(e) {
      if (e == 0) {
        this.pickShow = false
        this.addQuestionObj.answer_type = 2
      } else {
        this.pickShow = true
        this.addQuestionObj.answer_type = 1
      }
      this.pick.forEach((item) => {
        item.value = ''
      })
    },
    addQuestions() {
      this.$refs.dialog.validate(valid => {
        if (!valid) {
          return false
        }
        // 如果是选择题类型，就把选项答案用“-”拼接起来
        if (this.addQuestionObj.answer_type == 1) {
          var html = ''
          this.pick.forEach((item) => {
            if (item.value != '') {
              html += '-' + item.value
            }
          })
          this.addQuestionObj.answer = html.substring(1)
        }
        addQuestion(this.addQuestionObj).then(res => {
          this.$message.success(res.msg)
          this.dialogVisible = false
          this.addQuestionObj.answer = ''
          this.getQuestionList()
        }).catch(err => {
          this.dialogVisible = false
          this.addQuestionObj.answer = ''
        })
      })
    },
    hanldePreview(id) {
      detailQuestion({ question_id: id }).then(res => {
        this.addQuestionObj.question = res.data.question_info.question
        this.addQuestionObj.answer_type = res.data.question_info.answer_type
        this.addQuestionObj.pick_type = res.data.question_info.pick_type
        var list  = this.list
        var pick = []
        if(res.data.question_info.pick_type){
          res.data.answer_info.forEach((item, index)=>{
            // this.pick[index].value = item.content
            pick.push({'label':list[index],'value':item.content})
          })
        }
        this.pick = pick
        this.pickShow = true
        this.dialogVisible = true
        this.showBtn = false
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 如果点击了确定 则执行的逻辑
        delQuestion({ question_id: id }).then(res => {
          this.$message.success(res.msg)
          this.getQuestionList()
        })
      })
    },
    dialogClose() {
      this.pickShow = false
      this.showBtn = true
      this.pick.forEach((item) => {
        item.value = ''
      })
      this.addQuestionObj = {
        question: '',
        answer_type: 2,
        answer: '',
        pick_type: 0
      }
    }
  }
}
</script>

<style>
</style>
